﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using Newtonsoft.Json.Linq;
using TianYu.Core.Common;

namespace TianYu.Core.Pdf.ContentElement.Text
{
    /// <summary>
    /// 文本框，字段
    /// </summary>
    public class TextElement : AbstractTextElement
    {
        /// <summary> 字段 </summary>
        public string Field { get; set; }

        /// <summary> 标题 </summary>
        public TitleModel Title { get; set; }

        /// <summary> 自定义内容 </summary>
        public ContentModel Content { get; set; }


        public override void Draw(PdfTemplateContent pdfTemplateContent)
        {
            var pdfContentByte = pdfTemplateContent.PdfWriter.DirectContent;

            //文本
            var columnText = new ColumnText(pdfContentByte);

            //画矩形 
            base.DrawRectangle(columnText, pdfContentByte, Math.Abs(Content.Size - 12));

            Paragraph paragraph = new Paragraph(Title.RowSpace * CommonHelper.BaseLeading);

            var fontColor = (FontColor > 0) ? CommonHelper.GetBaseColor(FontColor) : BaseColor.BLACK;

            //标题
            if (Title != null && Title.IsShow)
            {
                var fontStyle = CommonHelper.GetFontStyle(Title.Style);
                Font family = new Font(CommonHelper.GetFontFamilyPath(Title.Family), Title.Size, fontStyle, fontColor);

                paragraph.SpacingBefore = 1;
                paragraph.Font = family;
                paragraph.Alignment = CommonHelper.GetAlignment(Title.Align);
                paragraph.Add(Title.Text);
            }

            //内容
            if (!string.IsNullOrEmpty(Field))
            {
                var fontStyle = CommonHelper.GetFontStyle(Content.Style);
                Font family = new Font(CommonHelper.GetFontFamilyPath(Content.Family), Content.Size, fontStyle, fontColor);

                paragraph.SpacingBefore = 1;
                paragraph.Leading = (Content.RowSpace > 1) ? Content.Size * Content.RowSpace : CommonHelper.BaseLeading * Content.RowSpace;
                paragraph.Font = family;
                paragraph.Alignment = CommonHelper.GetAlignment(Content.Align); 

                //数据源：正常；异常：页码等（对象数组》当前所在子项）  
                var text = string.Empty;
                if (Field.IndexOf('.') >= 0)
                {
                    var field = Field.Split('.')[1];

                    var dataFrom = JObject.FromObject(pdfTemplateContent.JToken);

                    text = dataFrom.StringValue(field);//取值  
                }
                else
                {
                    text = pdfTemplateContent.PdfObject.StringValue(Field);//取值  
                }

                //换行显示
                if (Title.IsFullRowShow)
                {
                    Paragraph paragraphTemp = paragraph;
                    paragraphTemp.Clear();//清空数据 

                    paragraphTemp.Add(text);
                    columnText.AddElement(paragraphTemp);
                }
                else
                {
                    paragraph.Add(text);

                    //pdfContentByte.BeginText();
                    //pdfContentByte.SetFontAndSize(CommonHelper.GetFontFamilyPath(Content.Family), Content.Size);
                    //pdfContentByte.SetTextMatrix(X, Y);
                    //pdfContentByte.ShowText(text);
                    //pdfContentByte.EndText();
                }
                columnText.AddElement(paragraph);
            }

            columnText.Go();
        }
    }
}
